/**
 * 
 */
package neptune.web.client.callback;

import neptune.web.client.WebInterface;
import neptune.web.client.model.ExperimentInfos;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ExperimentView;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.ViewManager;
import neptune.web.client.view.components.LongMessageWidget;
import neptune.web.client.view.components.MessageWidget;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.PopupPanel;

public class TopologyUploadCallback implements AsyncCallback<ExperimentInfos> {

	private String expName;
	private PopupPanel loadingPopup = null;

	public TopologyUploadCallback(String expName) {
		super();
		this.expName = expName;
	}

	public TopologyUploadCallback(String expName2, PopupPanel loadingPopup) {
		expName = expName2;
		this.loadingPopup = loadingPopup;
	}

	public void onSuccess(ExperimentInfos result) {

		if (loadingPopup != null)
			loadingPopup.hide();

		// Imposta lo stato dell'esperimento
		SuperView operative = (SuperView) ViewManager.getViewManager()
				.getViewByName(OperativeView.DEFAULT_TOKEN);

		ExperimentView expView = (ExperimentView) operative
				.getViewByName(ExperimentView.DEFAULT_TOKEN);

		expView.setExperimentState(result.experimentStatus);

		// Crea il messaggio sulla validazione della topologia
		String message = "Topology uploading completed.\n"
				+ "WARNING: Only if the validation process gives a VALID result "
				+ "the topology is actually saved on the server." + "\n\n\n"
				+ "Topology validation result: " + result.topologyStatus;
		WebInterface.showMessageWindow(new LongMessageWidget(
				MessageWidget.MESSAGE, message + "\nTopology validation log:\n"
						+ result.topologyLog), "Validation log", 500, 500);

		ConsoleManager.addConsoleMessage(message, false);
	}

	public void onFailure(Throwable caught) {
		if (loadingPopup != null)
			loadingPopup.hide();

		WebInterface.addMessageToConsoleAndShowDialog(
				"Error while validating topology for experiment '" + expName
						+ "'. Cause:\n" + caught.getMessage(), true);
	}
}